<?php
namespace app\common\service;

use think\Model;

class StoreService extends BaseService
{

    /**
     * 获取店铺基本信息
     * @param $sid
     * @return array|false|\PDOStatement|string|Model
     */
    public function getStoreBaseInfo($sid){
//        $info = db('store')->field('store_name,description,banner,brief_intro')->where(['store_id' => $sid, 'status' => 1])->find();
        $field = 's.store_name,s.description,s.banner,s.brief_intro,s.magazine_cate_id,mc.title as magazine_cate_name';
        $info = db('store s')
            ->field($field)
            ->join('magazine_category mc', 'mc.cate_id=s.magazine_cate_id', 'LEFT')
            ->where(['s.store_id' => $sid, 's.status' => 1])
            ->find();
        if(!$info){
            return false;
        }
        $info['banner'] = $info['banner'] ? json_decode($info['banner'], 1) : '';
        return $info;
    }

    /**
     * 后台: 编辑店铺基本信息
     * @param $sid
     * @param $description
     * @param $banner
     * @param $brief_intro
     * @return int|string
     * @throws \think\Exception
     */
    public function editStoreBaseInfo($sid, $description, $banner, $brief_intro, $magazine_cate_id){
        $data = [
            'description' => $description,
            'banner' => json_encode($banner),
            'brief_intro' => $brief_intro,
            'magazine_cate_id' => $magazine_cate_id
        ];
        return db('store')->where(['store_id' => $sid])->update($data);
    }

    /**
     * 微信端: 获取店铺简介及banner图
     * @param $sid
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getStoreDesc($sid, $uid){
        $info = $this->getStoreBaseInfo($sid);
        if(!$info){
            return false;
        }
        $info['magazine_cate_link'] = $info['magazine_cate_id'] ? $this->domain.'/m/html/magazine_list.html?sid='.$sid.'&uid='.$uid.'&cid='.$info['magazine_cate_id'] : '';

        //判断当前学员是否为正式学员，供分享用
        $is_counselor = db('user_integral')->where(['sid' => $sid, 'userid' => $uid])->value('counselor_status');
        if($is_counselor){
            $info['formal_status'] = 0;
        } else {
            $formal_status = db('students')->where(['sid' => $sid, 'uid' => $uid])->value('formal_status');
            $info['formal_status'] = $formal_status ?? 0;
        }

        return $info;
    }


    /**
     * 微信端: 通过页面分类ID获取最新活动
     * @param $sid
     * @param $uid
     * @param $magazine_cate_id
     * @return array
     */
    public function getLatestActivityByCateId($sid, $uid, $magazine_cate_id){
        $data = [];

        if(!$magazine_cate_id){
            return ['status' => 0, 'data' => $data, 'msg' => '未发布最新活动'];
        }

        $magazine = db('magazine')
                    ->field('mid,title,subtitle,describe,view_pic')
                    ->where(['sid' => $sid, 'category' => ['like', "%$magazine_cate_id%"], 'status' => 1])
                    ->order('create_time DESC')
                    ->limit(3)
                    ->select();

        if($magazine){
            //页面内容URL
            foreach($magazine as $k => $v){
                $magazine[$k]['url'] = $this->domain.'/m/html/magazine.html?sid='.$sid.'&uid='.$uid.'&id='.$v['mid'];
            }
            $data = ['activity' => $magazine];
        }

        return ['status' => 1, 'data' => $data, 'msg' => '获取成功'];
    }


    /**
     * 微信端: 获取最新活动
     * @param $sid
     * @param $uid
     * @return array
     */
    public function getLatestActivity($sid, $uid){
        $magazine_cate_id = db('store')
                            ->where(['store_id' => $sid, 'status' => 1])
                            ->value('magazine_cate_id');
        return $this->getLatestActivityByCateId($sid, $uid, $magazine_cate_id);
    }

    /**
     * 微信端: 获取店铺购买协议
     * @param $sid
     * @return mixed
     */
    public function getPurchaseContract($sid){
        return db('store')->where(['store_id' => $sid])->value('purchase_contract');
    }
}






















